package owl2vowl;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionGroup;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.io.FilenameUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.semanticweb.owlapi.model.IRI;

import owl2vowl.converter.IRIConverter;
import owl2vowl.export.types.ConsoleExporter;
import owl2vowl.export.types.Exporter;
import owl2vowl.export.types.FileExporter;

public class ConsoleMain {
	private static final String IRI_OPTION_NAME = "iri";
	private static final String FILE_OPTION_NAME = "file";
	private static final String OUTPUT_OPTION_NAME = "output";
	private static final String DEPENDENCIES_OPTION_NAME = "dependencies";
	private static final String HELP_OPTION_NAME = "h";
	private static final String ECHO_OPTION_NAME = "echo";
	private static final Logger logger = LogManager.getLogger(ConsoleMain.class);

	public static void main(String[] args) {
		(new ConsoleMain()).parseCommandLine(args);
	}

	protected void parseCommandLine(String[] args) {
		CommandLine line = null;
		Options options = this.createOptions();

		try {
			line = (new DefaultParser()).parse(options, args);
		} catch (ParseException arg11) {
			System.err.println("Parsing failed.  Reason: " + arg11.getMessage());
			this.printHelpMenu(options);
			System.exit(0);
		}

		if (line.hasOption("h")) {
			this.printHelpMenu(options);
			System.exit(0);
		}

		ArrayList dependencies = new ArrayList();
		IRI ontologyIri;
		if (line.hasOption("file")) {
			ontologyIri = IRI.create(new File(line.getOptionValue("file")));
		} else {
			String e = line.getOptionValue("iri");
			String out = e.replace(" ", "%20");
			ontologyIri = IRI.create(out);
		}

		if (line.hasOption("dependencies")) {
			String[] arg12 = line.getOptionValues("dependencies");
			int arg14 = arg12.length;

			for (int arg7 = 0; arg7 < arg14; ++arg7) {
				String path = arg12[arg7];
				File dependency = new File(path);
				dependencies.add(IRI.create(dependency));
			}
		}

		try {
			IRIConverter arg13 = new IRIConverter(ontologyIri, dependencies);
			arg13.convert();
			arg13.export(this.createExporterFromOption(line, ontologyIri));
		} catch (Exception arg10) {
			logger.error("FAILED TO LOAD " + Arrays.toString(args));
			System.err.println(arg10.getClass().getName());
			System.err.println(arg10.getMessage());
			System.exit(1);
		}

	}

	protected Options createOptions() {
		Options options = new Options();
		options.addOption(Option.builder("h").desc("views this help text").build());
		OptionGroup inputOptions = new OptionGroup();
		inputOptions.setRequired(true);
		inputOptions.addOption(Option.builder("iri").argName("IRI").hasArg().desc("the iri of an ontology").build());
		inputOptions.addOption(
				Option.builder("file").argName("PATH").hasArg().desc("the local path to an ontology").build());
		options.addOption(Option.builder("dependencies").argName("PATHS").hasArgs()
				.desc("paths to dependencies of a local ontology").build());
		OptionGroup outputOptions = new OptionGroup();
		outputOptions.addOption(Option.builder("echo").desc("prints the converted ontology on the console").build());
		outputOptions.addOption(Option.builder("output").argName("PATH").hasArg()
				.desc("specify the path for the desired output location").build());
		options.addOptionGroup(inputOptions);
		options.addOptionGroup(outputOptions);
		return options;
	}

	protected void printHelpMenu(Options options) {
		HelpFormatter helpFormatter = new HelpFormatter();
		helpFormatter.printHelp("java -jar owl2vowl.jar", options);
	}

	protected Exporter createExporterFromOption(CommandLine line, IRI ontologyIri) {
		if (line.hasOption("echo")) {
			return new ConsoleExporter();
		} else {
			String exportPath = null;
			if (line.hasOption("output")) {
				exportPath = line.getOptionValue("output");
			}

			return this.generateFileExporter(ontologyIri, exportPath);
		}
	}

	protected FileExporter generateFileExporter(IRI ontologyIri, String filePath) {
		String filename;
		if (filePath != null) {
			filename = filePath;
		} else {
			try {
				filename = FilenameUtils.removeExtension((String) ontologyIri.getRemainder().get()) + ".json";
			} catch (Exception arg5) {
				System.out.println("Failed to extract filename from iri");
				System.out.println("Reason: " + arg5);
				String defaultName = "default.json";
				System.out.println("Writing to \'" + defaultName + "\'");
				filename = defaultName;
			}
		}

		return new FileExporter(new File(filename));
	}
}